package com.moxiao.yfjh.module.activity.common;

import com.moxiao.yfjh.framework.common.util.date.DateUtils;
import com.moxiao.yfjh.module.activity.controller.admin.activityInfo.vo.ActivityInfoCreateReqVO;
import com.moxiao.yfjh.module.activity.controller.admin.merchanType.vo.MerchantTypeCreateReqVO;
import com.moxiao.yfjh.module.activity.controller.admin.merchantInfo.vo.MerchantInfoCreateReqVO;
import com.moxiao.yfjh.module.activity.controller.admin.packageInfo.vo.PackageInfoCreateReqVO;
import com.moxiao.yfjh.module.activity.controller.admin.packageType.vo.PackageTypeCreateReqVO;
import com.moxiao.yfjh.module.activity.controller.admin.productInfo.vo.ProductInfoCreateReqVO;
import com.moxiao.yfjh.module.activity.controller.admin.productType.vo.ProductTypeCreateReqVO;
import com.moxiao.yfjh.module.activity.dal.dataobject.activityInfo.ActivityInfoDO;
import com.moxiao.yfjh.module.activity.dal.dataobject.merchanType.MerchantTypeDO;
import com.moxiao.yfjh.module.activity.dal.dataobject.merchantInfo.MerchantInfoDO;
import com.moxiao.yfjh.module.activity.dal.dataobject.packageInfo.PackageInfoDO;
import com.moxiao.yfjh.module.activity.dal.dataobject.packageType.PackageTypeDO;
import com.moxiao.yfjh.module.activity.dal.dataobject.productInfo.ProductInfoDO;
import com.moxiao.yfjh.module.activity.dal.dataobject.productType.ProductTypeDO;
import com.moxiao.yfjh.module.activity.service.activityInfo.ActivityInfoService;
import com.moxiao.yfjh.module.activity.service.merchanType.MerchantTypeService;
import com.moxiao.yfjh.module.activity.service.merchantInfo.MerchantInfoService;
import com.moxiao.yfjh.module.activity.service.packageInfo.PackageInfoService;
import com.moxiao.yfjh.module.activity.service.packageType.PackageTypeService;
import com.moxiao.yfjh.module.activity.service.productInfo.ProductInfoService;
import com.moxiao.yfjh.module.activity.service.productType.ProductTypeService;
import com.moxiao.yfjh.module.pay.dal.dataobject.order.PayOrderDO;
import com.moxiao.yfjh.module.pay.service.order.PayOrderService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.util.Objects;

/**
 * @version 1.0
 * @Author zbq
 * @Date 二〇二四年二月二十九日
 **/
@Service
public class GenActiveNoCommonUtil {

    private static final String  PREFIX_MEMBER_ORDER = "HD";
    private static final String  PREFIX_SHANG_JIA_TYPE = "SJLX";
    private static final String  PREFIX_SHANG_JIA = "SJ";

    private static final String  PREFIX_SHANG_PIN = "HDSP";

    private static final String  PREFIX_SHANG_PIN_TYPE = "SPLX";

    private static final String  PREFIX_TAO_CAN_TYPE = "TCLX";

    private static final String  PREFIX_TAO_CAN = "TC";

    private static final String PAY_TAO_CAN = "PAY";
    @Resource
    private ActivityInfoService activityInfoService;

    @Resource
    private MerchantTypeService merchantTypeService;

    @Resource
    private MerchantInfoService merchantInfoService;

    @Resource
    private ProductTypeService productTypeService;

    @Resource
    private ProductInfoService productInfoService;

    @Resource
    private PackageTypeService packageTypeService;

    @Resource
    private PackageInfoService packageInfoService;

    @Resource
    private PayOrderService payOrderService;

    /**
     * 增加 userNo(用户编号)字段
     *
     * @param createReqVO
     */
    public void addActiveNo(ActivityInfoCreateReqVO createReqVO) {
        ActivityInfoDO activityInfoDO = activityInfoService.getActivityTodayLast(DateUtils.getTodayStart(), DateUtils.getTodayEnd());
        DecimalFormat formatCount = new DecimalFormat("0000");
        int dateTime = Integer.parseInt(DateUtils.dateTime());
        if (Objects.nonNull(activityInfoDO)) {
            String[] user_no = activityInfoDO.getNo().split("_");
            String lastPart = user_no[user_no.length - 1];
            if (user_no.length > 1) {
                Integer num = Integer.parseInt(lastPart) + 1;
                String no = formatCount.format(num);
                createReqVO.setNo(PREFIX_MEMBER_ORDER + "_" + dateTime + "_" + no);
            }
        }else {
            String order_no = PREFIX_MEMBER_ORDER + "_" + dateTime + "_" + "0001";
            Integer num = activityInfoService.getActiveNo(order_no);
            if (num == 0) {
                createReqVO.setNo(order_no);
            }
        }
    }


    public void addMerchantTypeNo(MerchantTypeCreateReqVO createReqVO){

        MerchantTypeDO merchantTypeDO = merchantTypeService.getMerchantTypeTodayLast(DateUtils.getTodayStart(), DateUtils.getTodayEnd());
        DecimalFormat formatCount = new DecimalFormat("0000");
        int dateTime = Integer.parseInt(DateUtils.dateTime());
        if (Objects.nonNull(merchantTypeDO)){
            String[] user_no = merchantTypeDO.getNo().split("_");
            String lastPart = user_no[user_no.length - 1];
            if (user_no.length > 1) {
                Integer num = Integer.parseInt(lastPart) + 1;
                String no = formatCount.format(num);
                createReqVO.setNo(PREFIX_SHANG_JIA_TYPE + "_" + dateTime + "_" + no);
            }
        }else {
            String order_no = PREFIX_SHANG_JIA_TYPE + "_" + dateTime + "_" + "0001";
            Integer num = merchantTypeService.getMerchantTypeNo(order_no);
            if (num == 0) {
                createReqVO.setNo(order_no);
            }
        }


    }



    public void addMerchantInfoNo(MerchantInfoCreateReqVO createReqVO){

        MerchantInfoDO merchantInfoDO = merchantInfoService.getMerchantInfoTodayLast(DateUtils.getTodayStart(), DateUtils.getTodayEnd());
        DecimalFormat formatCount = new DecimalFormat("0000");
        int dateTime = Integer.parseInt(DateUtils.dateTime());
        if (Objects.nonNull(merchantInfoDO)){
            String[] user_no = merchantInfoDO.getNo().split("_");
            String lastPart = user_no[user_no.length - 1];
            if (user_no.length > 1) {
                Integer num = Integer.parseInt(lastPart) + 1;
                String no = formatCount.format(num);
                createReqVO.setNo(PREFIX_SHANG_JIA + "_" + dateTime + "_" + no);
            }
        }else {
            String order_no = PREFIX_SHANG_JIA + "_" + dateTime + "_" + "0001";
            Integer num = merchantInfoService.getMerchantInfoNo(order_no);
            if (num == 0) {
                createReqVO.setNo(order_no);
            }
        }

    }

    public void addProductTypeNo(ProductTypeCreateReqVO createReqVO){

        ProductTypeDO productTypeDO = productTypeService.getProductTypeTodayLast(DateUtils.getTodayStart(), DateUtils.getTodayEnd());
        DecimalFormat formatCount = new DecimalFormat("0000");
        int dateTime = Integer.parseInt(DateUtils.dateTime());
        if (Objects.nonNull(productTypeDO)){
            String[] user_no = productTypeDO.getNo().split("_");
            String lastPart = user_no[user_no.length - 1];
            if (user_no.length > 1) {
                Integer num = Integer.parseInt(lastPart) + 1;
                String no = formatCount.format(num);
                createReqVO.setNo(PREFIX_SHANG_PIN_TYPE + "_" + dateTime + "_" + no);
            }
        }else {
            String order_no = PREFIX_SHANG_PIN_TYPE + "_" + dateTime + "_" + "0001";
            Integer num = productTypeService.getProductTypeNo(order_no);
            if (num == 0) {
                createReqVO.setNo(order_no);
            }
        }


    }



    public void addProductInfoNo(ProductInfoCreateReqVO createReqVO){

        ProductInfoDO productInfoDO = productInfoService.getProductInfoTodayLast(DateUtils.getTodayStart(), DateUtils.getTodayEnd());
        DecimalFormat formatCount = new DecimalFormat("0000");
        int dateTime = Integer.parseInt(DateUtils.dateTime());
        if (Objects.nonNull(productInfoDO)){
            String[] user_no = productInfoDO.getNo().split("_");
            String lastPart = user_no[user_no.length - 1];
            if (user_no.length > 1) {
                Integer num = Integer.parseInt(lastPart) + 1;
                String no = formatCount.format(num);
                createReqVO.setNo(PREFIX_SHANG_PIN + "_" + dateTime + "_" + no);
            }
        }else {
            String order_no = PREFIX_SHANG_PIN + "_" + dateTime + "_" + "0001";
            Integer num = productInfoService.getProductInfoNo(order_no);
            if (num == 0) {
                createReqVO.setNo(order_no);
            }
        }

    }


    public void addPackageTypeNo(PackageTypeCreateReqVO createReqVO){

        PackageTypeDO packageTypeDO = packageTypeService.getPackageTypeTodayLast(DateUtils.getTodayStart(), DateUtils.getTodayEnd());
        DecimalFormat formatCount = new DecimalFormat("0000");
        int dateTime = Integer.parseInt(DateUtils.dateTime());
        if (Objects.nonNull(packageTypeDO)){
            String[] user_no = packageTypeDO.getNo().split("_");
            String lastPart = user_no[user_no.length - 1];
            if (user_no.length > 1) {
                Integer num = Integer.parseInt(lastPart) + 1;
                String no = formatCount.format(num);
                createReqVO.setNo(PREFIX_TAO_CAN_TYPE + "_" + dateTime + "_" + no);
            }
        }else {
            String order_no = PREFIX_TAO_CAN_TYPE + "_" + dateTime + "_" + "0001";
            Integer num = packageTypeService.getPackageTypeNo(order_no);
            if (num == 0) {
                createReqVO.setNo(order_no);
            }
        }


    }


    public void addPackageInfoNo(PackageInfoCreateReqVO createReqVO){

        PackageInfoDO packageInfoDO = packageInfoService.getPackageInfoTodayLast(DateUtils.getTodayStart(), DateUtils.getTodayEnd());
        DecimalFormat formatCount = new DecimalFormat("0000");
        int dateTime = Integer.parseInt(DateUtils.dateTime());
        if (Objects.nonNull(packageInfoDO)){
            String[] user_no = packageInfoDO.getNo().split("_");
            String lastPart = user_no[user_no.length - 1];
            if (user_no.length > 1) {
                Integer num = Integer.parseInt(lastPart) + 1;
                String no = formatCount.format(num);
                createReqVO.setNo(PREFIX_TAO_CAN + "_" + dateTime + "_" + no);
            }
        } else {
            String order_no = PREFIX_TAO_CAN + "_" + dateTime + "_" + "0001";
            Integer num = packageInfoService.getPackageInfoNo(order_no);
            if (num == 0) {
                createReqVO.setNo(order_no);
            }
        }

    }

    /**
     * 生成支付单号
     */


    /**
     * 增加 userNo(用户编号)字段
     *
     * @param createReqVO
     */
    public void addPayNo(PayOrderDO createReqVO) {
        PayOrderDO payOrderDO = payOrderService.getActivityTodayLast(DateUtils.getTodayStart(), DateUtils.getTodayEnd());
        DecimalFormat formatCount = new DecimalFormat("0000");
        int dateTime = Integer.parseInt(DateUtils.dateTime());
        if (Objects.nonNull(payOrderDO)) {
            String[] user_no = payOrderDO.getNo().split("_");
            String lastPart = user_no[user_no.length - 1];
            if (user_no.length > 1) {
                Integer num = Integer.parseInt(lastPart) + 1;
                String no = formatCount.format(num);
                createReqVO.setNo(PAY_TAO_CAN + "_" + dateTime + "_" + no);
            }
        } else {
            String order_no = PAY_TAO_CAN + "_" + dateTime + "_" + "0001";
            Integer num = payOrderService.getNo(order_no);
            if (num == 0) {
                createReqVO.setNo(order_no);
            }
        }
    }
}
